package com.tools.cleanmaster;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Message;

import com.hm.base.android.mob.AMApplication;
import com.tools.acceleratecore.AccelerateCoreFactory;
import com.tools.ad.ADModule;
import com.tools.ad.manager.AdSceneManager;
import com.tools.cleanmaster.appinfo.AppManager;
import com.tools.cleanmaster.cache.CleanRawCache;
import com.tools.cleanmaster.common.thread.ThreadPool;
import com.tools.cleanmaster.cpucool.CpuCoolManager;
import com.tools.cleanmaster.hijack.HijackLibrary;
import com.tools.cleanmaster.manager.AutoCleanManager;
import com.tools.cleanmaster.manager.CandySaverManager;
import com.tools.cleanmaster.manager.ChargingManager;
import com.tools.cleanmaster.manager.CleanBeanManager;
import com.tools.cleanmaster.manager.NotificationSceneManager;
import com.tools.cleanmaster.model.cloud.TrashQueryPathManager;
import com.tools.cleanmaster.service.StickService;
import com.tools.cleanmaster.store.CleanSharedPrefManager;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.taskmain.AppTaskUtils;
import com.tools.cleanmaster.ui.activity.SmartChargingActivity;
import com.tools.cleanmaster.utils.WebViewFixer;

public abstract class CleanApplication extends AMApplication {
    private static final String TAG = "CleanApplication";

    private static CleanApplication sInstance;

    public static CleanApplication getInstance() {
        return sInstance;
    }

    @Override
    protected void initMApplication() throws Exception {
        super.initMApplication();

        sInstance = this;

        CleanConstant.init(this);
        WebViewFixer.apply(this);
        HijackLibrary.hijack(this);

        ThreadPool.startup();
        ThreadPool.runOnPool(new Runnable() {
            @Override
            public void run() {
                registrationRepoEvent();
            }
        });
        AppManager.getInstance(this).listenSystemBroadcasts();

        initWhiteData();

        // so 文件经常获取不了，所以这个优先捕获异常。
        try {
            AccelerateCoreFactory.setApplication(this);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }

        CpuCoolManager.getInstance().initManager();
        TrashQueryPathManager.getInstance().startUpdataTask();
        GlobalConfigSharedPrefManager.setInstallTime(this);

        NotificationSceneManager.getInstance();
        getAutoCleanManager().init();
        CandySaverManager.getInstance(this).start();

        initStickService();

    }

    protected void initStickService() {
        Intent intent = new Intent(this, StickService.class);
        startService(intent);
    }

    @Override
    protected void subSwitchHandleMessage(Message msg) {
        if (msg.what == R.id.msg_cleaner_show_smart_charging_view) {
            if (getCleanSharedPrefManager().getChargingEnable()) {
                Intent smartChargingIntent = new Intent(this, SmartChargingActivity.class);
                smartChargingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                smartChargingIntent.putExtra(SmartChargingActivity.SMART_CHARGING_LAUNCH_FLAG, msg.arg2);
                startActivity(smartChargingIntent);
            }
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        CandySaverManager.getInstance(this).stop();
    }

    private void initWhiteData() {
        ThreadPool.runOnWorker(new Runnable() {
            @Override
            public void run() {
                AppTaskUtils.loadAutoProtectedList();
                AppTaskUtils.loadSysProtectedList();
            }
        });
    }

    /**
     * 每隔24小时上报一次 不管有无进入大师主界面 只要运行过
     */
    private void registrationRepoEvent() {
        Intent intent = new Intent(CleanConstant.ACTION_ALIVE_RECEIVER);
        intent.putExtra(CleanConstant.ALIVE_RECEIVER_FROM, CleanConstant.ALIVE_RECEIVER_FROM_ALARM);
        PendingIntent pi = PendingIntent.getBroadcast(sInstance, 0, intent, 0);
        // 设置一个PendingIntent对象，发送广播
        AlarmManager am = (AlarmManager) sInstance.getSystemService(Context.ALARM_SERVICE);
        // 获取AlarmManager对象
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10 * 1000, 12 * CleanConstant.TIME_ONE_HOUR, pi);
    }

    public ADModule getADModule() {
        return (ADModule) getSubModule(ADModule.MODULE_KEY);
    }

    public AdSceneManager getAdSceneManager() {
        return ((ADModule) getSubModule(ADModule.MODULE_KEY)).getAdSceneManager();
    }

    public CleanSharedPrefManager getCleanSharedPrefManager() {
        return ((CleanBeanManager) getBeanManager()).getCleanSharedPrefManager();
    }

    public ChargingManager getChargingManager() {
        return ((CleanBeanManager) getBeanManager()).getChargingManager();
    }

    public AutoCleanManager getAutoCleanManager() {
        return ((CleanBeanManager) getBeanManager()).getAutoCleanManager();
    }

    public CleanRawCache getCleanRawCache() {
        return ((CleanBeanManager) getBeanManager()).getCleanRawCache();
    }

}
